if(tableeditor == undefined){
	var tableeditor = {};
}
/**
 * 用户自己通过tableeditor.addButton添加的自定义的type=1的button对象和button的onclick事件处理函数列表(二维数组)
 */
tableeditor.extraButtons = [];
/**
 * 按钮点击回调函数hash对象
 */
tableeditor.buttonOnclickCBFunctions = {};

tableeditor.checkAll = function(target){
	if(tableeditor.isUseCheckbox){
		var headCheckbox = target;
		var isChecked = headCheckbox.checked;
		for(var i=0; i<tableeditor.maxRowNum; i++){
			var e = document.getElementById("_tableeditor_checkbox" + i);
			if(e){
				e.checked = isChecked;
			}
		}
		tableeditor.onCheckboxClick(target);
	}
}
tableeditor.uncheckAll = function(){
	if(tableeditor.isUseCheckbox){
		document.getElementById("_tableeditor_checkbox").checked = false;
		for(var i=0; i<tableeditor.maxRowNum; i++){
			var e = document.getElementById("_tableeditor_checkbox" + i);
			if(e){
				e.checked = false;
			}
		}
	}
}
tableeditor.onCheckboxClick = function(target){
	var buttonView = document.getElementById("_tableeditor_button_view");
	var buttonEdit = document.getElementById("_tableeditor_button_edit");
	var buttonDelete = document.getElementById("_tableeditor_button_delete");
	var buttonReset = document.getElementById("_tableeditor_button_reset");  
	var isChecked = false;
	if(tableeditor.getCheckedRowIndex() != -1){
		isChecked = true;
	}

	if(isChecked){
		var t;
		for(var i=0; i<tableeditor.extraButtons.length; i++){
			t = tableeditor.extraButtons[i];
			t[0].className = "button";
			t[0].disabled = false;
			t[0].onclick = t[1];
		}
		if(buttonView){
			buttonView.className = "button";
			buttonView.disabled = false;
			buttonView.onclick = tableeditor.onViewButtonClick;
		}
		if(buttonEdit){
			buttonEdit.className = "button";
			buttonEdit.disabled = false;
			if(buttonReset){
				buttonReset.className = "button";
				buttonReset.disabled = false;
			}
			buttonEdit.onclick = tableeditor.onEditButtonClick;
		}
		if(buttonDelete){
			buttonDelete.className = "button";
			buttonDelete.disabled = false;
			buttonDelete.onclick = tableeditor.onDeleteButtonClick;
		}
	}else{
		var t;
		for(var i=0; i<tableeditor.extraButtons.length; i++){
			t = tableeditor.extraButtons[i];
			t[0].className = "buttonDisabled";
			t[0].disabled = true;
			t[0].onclick = null;
		}
		if(buttonView){
			buttonView.className = "buttonDisabled";
			buttonView.disabled = true;
			buttonView.onclick = null;
		}
		if(buttonEdit){
			buttonEdit.className = "buttonDisabled";
			buttonEdit.disabled = true;
			buttonEdit.onclick = null;
		}
		if(buttonDelete){
			buttonDelete.className = "buttonDisabled";
			buttonDelete.disabled = true;
			buttonDelete.onclick = null;
		}
	}
}
tableeditor.onRowClick = function(tr){
	tableeditor.clearTip();
	tableeditor.uncheckAll();
	var cells = tr.cells;
	var checkbox = cells[0].childNodes[0];
	if(checkbox.type == 'checkbox'){
		if(checkbox.checked){
			checkbox.checked = false;
		}else{
			checkbox.checked = true;
		}
	}
	tableeditor.onCheckboxClick(checkbox);
	var clickedRowIndex = tableeditor.getClickedRowIndex(tr);

	tableeditor.fillFormForEdit(clickedRowIndex);
	tableeditor.hightEdittingRow();
	
	var row = tableeditor.list[clickedRowIndex];
	var isUseSubField = tableeditor.isUseSubField();
	var subField = tableeditor.IDFieldName;
	if(isUseSubField){
		subField = Utils.getSubField(tableeditor.IDFieldName);
	}
	var objId = row[subField];
	
	var CB = tableeditor.buttonOnclickCBFunctions['edit'];
	if(CB){
		CB(objId);
	}
	//show child list
	tableeditor.showChild(clickedRowIndex);
}
tableeditor.trOnMouseOver = function(target){
	if(target.style.backgroundColor == ""){
		target.style.backgroundColor = "#b6caea";
	}
}
tableeditor.trOnMouseOut = function(target){
	if(target.style.backgroundColor == "rgb(182, 202, 234)"
		|| target.style.backgroundColor == "#b6caea"){
		target.style.backgroundColor = "";
	}
}

tableeditor.addNewRow = function(index, row, isChecked, isRowUseSubField){
	if(isRowUseSubField == undefined){
		isRowUseSubField = false;
	}
	var table = jQuery("#_tableeditor_dataTable")[0];
	var nTR = table.insertRow(index);
	nTR.onmouseover = tableeditor.trOnMouseOver.bind(this, nTR);
	nTR.onmouseout = tableeditor.trOnMouseOut.bind(this, nTR);
	nTR.ondblclick = tableeditor.onRowClick.bind(this, nTR);
	if(tableeditor.isUseCheckbox){
		var nTD = nTR.insertCell(-1);
		nTD.className = "first";
		nTD.innerHTML = '<input id="_tableeditor_checkbox' + (tableeditor.maxRowNum) + '" checked="' + isChecked + '" name="_tableeditor_checkbox' + (tableeditor.maxRowNum) + '" onclick="tableeditor.onCheckboxClick(this);" type="checkbox"/>';
		$('_tableeditor_checkbox' + tableeditor.maxRowNum).checked = isChecked;
	}
	
	var isUseSubField = tableeditor.isUseSubField();
	var model;
	var id;
	
	for(var i=0; i<tableeditor.columns.length; i++){
		var column = tableeditor.columns[i];
		model = tableeditor.columnTds[i];
		var nTD = model.cloneNode(true);
		var t = '';
		try{
			if(isRowUseSubField){
				var subField = column;
				if(isUseSubField){
					subField = Utils.getSubField(column);
				}
				t = eval("(row." + subField + ")");
			}else{
				t = eval("(row." + column + ")");
			}
			//record the id
			if(column == tableeditor.IDFieldName){
				id = t;
			}
			if(tableeditor.isDateStr(t)){
				var columnDateFormatIndex = tableeditor.allColumns.indexOf(column);
				t = tableeditor.formatDateStr(t, tableeditor.allColumnDateFormats[columnDateFormatIndex]);
			}
		}catch(e){}
		if(t == undefined){
			t = "&nbsp;";
		}else{
			t = "&nbsp;" + t;
		}
		var columnInnerHTML = tableeditor.columnInnerHTMLs[i];
		if(columnInnerHTML != ''){
			columnInnerHTML = tableeditor.fillIdParameter(columnInnerHTML, id);
		}
		nTD.innerHTML = columnInnerHTML + t;
		nTR.appendChild(nTD);
	}
	if(tableeditor.changeOrderUrl != "null"){
		var nTD = document.createElement("TD");
		nTD.innerHTML = '<img onclick="tableeditor.changeRowOrder(' + (index-1) + ', -1);" src="' + tableeditor.upArrowImgSrc + '"/>  <img onclick="tableeditor.changeRowOrder(' + (index-1) + ', 1);" src="' + tableeditor.downArrowImgSrc + '"/>';
		nTR.appendChild(nTD);
	}
	tableeditor.maxRowNum++;
	
}
/**
 * @param dataType
 * 			1: 数据来自action返回
 * 			2: 数据来自tableeditor.list
 */
tableeditor.updateRow = function(index, data, dataType){
	if(dataType == undefined){
		dataType = 1;
	}
	var rows = jQuery("#_tableeditor_dataTable")[0].rows;
	if(rows.length < index + 1){
		alert(tableeditor.updateIndexOutOfRangeMessage + "index = " + (index + 1) + ", rows.length = " + rows.length);
	}else{
		if(index <= -1){
			index = 0;
		}
	}
	var updateRow = rows[index + 1];
	var column;
	
	var isUseSubField = tableeditor.isUseSubField();
	for(var i=0; i<tableeditor.columns.length; i++){
		column = tableeditor.columns[i];
		var TD = updateRow.cells[i + 1];
		var t;
		try{
			if(dataType == 1){
				t = eval("(data." + column + ")");
			}else{
				var subField = column;
				if(isUseSubField){
					subField = Utils.getSubField(column);
				}
				t = eval("(data." + subField + ")");
			}
		}catch(e){
			t = "";
		}
		if(tableeditor.isDateStr(t)){
			var columnDateFormatIndex = tableeditor.allColumns.indexOf(column);
			t = tableeditor.formatDateStr(t, tableeditor.allColumnDateFormats[columnDateFormatIndex]);
		}
		if(t == undefined){
			t = "&nbsp;";
		}else{
			t = "&nbsp;" + t;
		}
		TD.innerHTML = t;
	}
}
tableeditor.getPackageName = function(){
	var result = null;
	for(var i=0; i<tableeditor.columns.length; i++){
		column = tableeditor.columns[i];
		var tPackageName = Utils.getPackageName(column);
		if(tPackageName == null){
			result = null;
			break;
		}
		if(result == null || result == tPackageName){
			result = tPackageName;
		} 
	}
	return result;
}
tableeditor.deleteRow = function(index){
	var table = jQuery("#_tableeditor_dataTable")[0];
	table.deleteRow(index + 1);
	tableeditor.changePageInfo(-1);
}
tableeditor.onNewButtonClick = function(){
	tableeditor.clearTip();
	if($('_tableeditor_button_create')){
		$('_tableeditor_button_create').style.display = "";
	}
	if($('_tableeditor_button_update')){
		$('_tableeditor_button_update').style.display = "none";
	}
	if($('_tableeditor_button_new')){
		$('_tableeditor_button_new').style.display = "none";
	}
	if($('_tableeditor_button_reset')){
		$("_tableeditor_button_reset").onclick = tableeditor.onResetButtonClick.bind(this, 1);
	}
	if($(tableeditor.id)){
		var idInput = $(tableeditor.id)[tableeditor.IDFieldName];
		if(idInput){
			tableeditor.disableIdInput(true);
			idInput.value=tableeditor.autoGenerateMessage;
		}
	}
	tableeditor.unhightAllRow();
	tableeditor.resetForm(1);
	var CB = tableeditor.buttonOnclickCBFunctions['new'];
	if(CB){
		CB();
	}
}
tableeditor.onResetButtonClick = function(type){
	tableeditor.resetForm(type);
	var CB = tableeditor.buttonOnclickCBFunctions['reset'];
	if(CB){
		CB(type);
	}
}

tableeditor.onEditButtonClick = function(){
	tableeditor.clearTip();
	
	var checkedRowIndex = tableeditor.getCheckedRowIndex();
	if(checkedRowIndex == -1){
		alert(tableeditor.checkItemToEditMessage);
		return;
	}
	tableeditor.fillFormForEdit(checkedRowIndex);
	tableeditor.hightEdittingRow();
	var row = tableeditor.list[checkedRowIndex];
	var isUseSubField = tableeditor.isUseSubField();
	var subField = tableeditor.IDFieldName;
	if(isUseSubField){
		subField = Utils.getSubField(tableeditor.IDFieldName);
	}
	var objId = row[subField];
	
	var CB = tableeditor.buttonOnclickCBFunctions['edit'];
	if(CB){
		CB(objId);
	}
}
tableeditor.onViewButtonClick = function(){
	tableeditor.clearTip();
	var checkedRowIndex = tableeditor.getCheckedRowIndex();
	if(checkedRowIndex == -1){
		alert(tableeditor.checkItemToEditMessage);
		return;
	}
	tableeditor.fillFormForEdit(checkedRowIndex);
	tableeditor.hightEdittingRow();
	var row = tableeditor.list[checkedRowIndex];
	var isUseSubField = tableeditor.isUseSubField();
	var subField = tableeditor.IDFieldName;
	if(isUseSubField){
		subField = Utils.getSubField(tableeditor.IDFieldName);
	}
	var objId = row[subField];
	
	var CB = tableeditor.buttonOnclickCBFunctions['view'];
	if(CB){
		CB(objId);
	}
}
tableeditor.hightEdittingRow = function(){
	tableeditor.unhightAllRow();
	var checkedRow = tableeditor.getCheckedRow();
	if(checkedRow)
		checkedRow.style.backgroundColor = "#339999";
}
tableeditor.unhightAllRow = function(){
	var rows = $("_tableeditor_dataTable").rows;
	for(var i=0; i<rows.length; i++){
		rows[i].style.backgroundColor = "";
	}
}
tableeditor.fillFormForEdit = function(checkedRowIndex){
	if($("_tableeditor_button_create"))
		$("_tableeditor_button_create").style.display = "none";
	if($("_tableeditor_button_update")){
		$("_tableeditor_button_update").style.display = "";
	}
	if($("_tableeditor_button_new")){
		$("_tableeditor_button_new").style.display = "";
	}
	if($("_tableeditor_button_reset")){
		$("_tableeditor_button_reset").onclick = tableeditor.onResetButtonClick.bind(this, 2);
	}
	var form = jQuery("#" + tableeditor.id)[0];
	if(form == undefined){
		return;
	}
	tableeditor.disableIdInput(true);
	var row = tableeditor.list[checkedRowIndex];
	
	var formElements = form.elements;
	tableeditor.resetForm(1);
	var i=0;
	var isUseSubField = tableeditor.isUseSubField();
	for(var i=0; i<formElements.length; i++){
		var e = formElements[i];
		if(tableeditor.allColumns.indexOf(e.name) != -1){
			var subField;
			if(isUseSubField){
				subField = Utils.getSubField(e.name);
			}else{
				subField = e.name;
			}
			try{
				var value = eval("(row." + subField + ")");
				if(value != undefined){
					if(tableeditor.isDateStr(value)){
						var columnDateFormatIndex = tableeditor.allColumns.indexOf(e.name);
						value = tableeditor.formatDateStr(value, tableeditor.allColumnDateFormats[columnDateFormatIndex]);
					}
					if(typeof value == "object"){
						for(p in value){
							if(e.type.toLowerCase() == "checkbox" || e.type.toLowerCase() == "radio"){
								if(e.value == (value[p] + "")){
									e.checked = true;
									break;
								}else{
									e.checked = false;
								}
							}else{
								e.value = value[p];
								break;
							}
						}
					}else{
						if(e.type.toLowerCase() == "checkbox" || e.type.toLowerCase() == "radio"){
							if(e.value == (value + "")){
								e.checked = true;
							}else{
								e.checked = false;
							}
						}else{
							e.value = value;
						}
					}
				}
			}catch(e){}
		}
	}
}
tableeditor.onCreateButtonClick = function(){
	tableeditor.clearTip();
	if(tableeditor.validateCreateFunction != undefined && !tableeditor.validateCreateFunction()){
		return false;
	}
	$('_tableeditor_button_create').disabled = true;
	jQuery("#" + tableeditor.id)[0].action = tableeditor.createUrl + '?pageNum=' + tableeditor.pageNum + '&pageSize=' + tableeditor.pageSize;
	var options = {
		success:
			function(dataFromServer){ 
				dataFromServer = eval("(" + dataFromServer + ")");
				var data;
				if(dataFromServer[0] == "success"){
					
					var row = dataFromServer[4];
					var packageName = tableeditor.getPackageName();
					if(packageName == null){
						data = row;
					}else{
						data = eval("(row." + packageName + ")");
					}
					tableeditor.list.push(data);
					tableeditor.addNewRow(tableeditor.list.length, row, false);
					
					if(row.list != null){
						tableeditor.list = row.list;
						tableeditor.reshowList();
					}
					var totalNum = row.totalNum;
					var offset = 1;
					if(totalNum != undefined){
						offset = totalNum - tableeditor.totalNum;
					}
					tableeditor.changePageInfo(offset);
					var form = jQuery("#" + tableeditor.id)[0];
					tableeditor.resetForm(1);
				}else{
					tableeditor.showTip(dataFromServer);
				}
				$('_tableeditor_button_create').disabled = false;
				var CB = tableeditor.buttonOnclickCBFunctions['create'];
				if(CB){
					CB(data);
				}
		},
		type: "post"
	}
	jQuery("#" + tableeditor.id).ajaxSubmit(options);
}
tableeditor.onUpdateButtonClick = function(){
	tableeditor.clearTip();
	if(tableeditor.validateUpdateFunction != undefined && !tableeditor.validateUpdateFunction()){
		return false;
	}
	$('_tableeditor_button_update').disabled = true;
	var url = tableeditor.updateUrl;
	if(tableeditor.updateUrl.indexOf("?") == -1){
		url += "?";
	}else{
		url += "&";
	}
	url += 'pageNum=' + tableeditor.pageNum + '&pageSize=' + tableeditor.pageSize;
	jQuery("#" + tableeditor.id)[0].action = url;
	tableeditor.disableIdInput(false);
	var options = {
		beforeSubmit: 
			function(){
				tableeditor.disableIdInput(true);
			},
		success:
			function(dataFromServer){ 
				dataFromServer = eval("(" + dataFromServer + ")");
				tableeditor.disableIdInput(false);
				if(dataFromServer[0] == "success"){
					var checkedRowIndex = tableeditor.getCheckedRowIndex();
					var data = dataFromServer[4];
					if(data.list == null){
						tableeditor.updateRow(checkedRowIndex, data);
						var packageName = tableeditor.getPackageName();
						if(packageName == null){
							tableeditor.list[checkedRowIndex] = data;
						}else{
							tableeditor.list[checkedRowIndex] = eval("(data." + packageName + ")");
						}
					}else{
						tableeditor.list = data.list;
						tableeditor.reshowList();
					}
					tableeditor.onNewButtonClick();
					var totalNum = data.totalNum;
					var offset = 0;
					if(totalNum != undefined){
						offset = totalNum - tableeditor.totalNum;
					}
					tableeditor.changePageInfo(offset);
				}else{
					tableeditor.showTip(dataFromServer);
				}
				$('_tableeditor_button_update').disabled = false;
				tableeditor.disableIdInput(true);
				var CB = tableeditor.buttonOnclickCBFunctions['update'];
				if(CB){
					CB();
				}
			},
		type:"post"
		}
	jQuery("#" + tableeditor.id).ajaxSubmit(options);
}
tableeditor.onDeleteButtonClick = function(){
	if(!window.confirm(tableeditor.confirmDeleteMessage)){
		return;
	}
	tableeditor.clearTip();
	if(tableeditor.validateDeleteFunction != undefined && !tableeditor.validateDeleteFunction()){
		return false;
	}
	var checkedRowIndexs = tableeditor.getAllCheckedRowIndexs();
	if(checkedRowIndexs.length == 0){
		alert(tableeditor.checkItemToDeleteMessage);
		return;
	}
	var ids = [];
	var indexs = [];
	var isUseSubField = tableeditor.isUseSubField();
	for(var i=0; i<checkedRowIndexs.length; i++){
		var checkedRowIndex = checkedRowIndexs[i];
		var data = tableeditor.list[checkedRowIndex];
		
		var subField;
		if(isUseSubField){
			subField = Utils.getSubField(tableeditor.IDFieldName);
		}else{
			subField = tableeditor.IDFieldName;
		}
		var id = eval("data." + subField);
		ids.push(id);
		indexs.push(checkedRowIndex);
	}
	var postData = {};
	postData[Utils.getLastField(tableeditor.IDFieldName) + "s"] = ids;	
	
	
	var url = tableeditor.deleteUrl;
	if(tableeditor.updateUrl.indexOf("?") == -1){
		url += "?";
	}else{
		url += "&";
	}
	url += 'pageNum=' + tableeditor.pageNum + '&pageSize=' + tableeditor.pageSize;
	
	jQuery.post(
			url,
			postData,
			function(dataFromServer){
				dataFromServer = eval("(" + dataFromServer + ")");
				if(dataFromServer[0] == "success"){
					var data = dataFromServer[4];
					if(data.list == null){
						for(var i=0; i<indexs.length; i++){
							tableeditor.deleteRow(indexs[i] - i);
							tableeditor.list.splice(indexs[i] - i, 1);
						}
					}else{
						tableeditor.changePageInfo(indexs.length * -1);
						tableeditor.list = data.list;
						tableeditor.reshowList();
					}
					for(var i=0; i<indexs.length; i++){
						var CB = tableeditor.buttonOnclickCBFunctions['delete'];
						if(CB){
							CB(ids);
						}
					}
					tableeditor.onNewButtonClick();
					tableeditor.onCheckboxClick();
				}else{
					tableeditor.showTip(dataFromServer);
				}
			}

	);
}
tableeditor.clearTip = function(){
	var form = jQuery("#" + tableeditor.id)[0];
	if(form){
		validation.clearErrorMessages(form);
		validation.clearErrorLabels(form);
		validation.clearActionMessagesAndErrors(tableeditor.id);
	}
}
tableeditor.showTip = function(dataFromServer){
	var form = jQuery("#" + tableeditor.id)[0];
	if(form){
		var actionMessages = dataFromServer[1];
		var actionErrors = dataFromServer[2];
		var fieldErrors = dataFromServer[3];
		validation.addActionErrors(dataFromServer[2], tableeditor.id);
		validation.addActionMessages(dataFromServer[1], tableeditor.id);
		validation.addFieldErrors(dataFromServer[3], tableeditor.id);
	}else{
		Utils.showAjaxTip(dataFromServer);
	}
}

tableeditor.getCheckedRowIndex = function(){
	var result = -1; 
	var table = $("_tableeditor_dataTable");
	var checkBoxs = table.getElementsByTagName("input");
	for(var i=1; i<checkBoxs.length; i++){
		var e = checkBoxs[i];
		if(e.checked){
			result = i - 1;
			break;
		}
	}
	return result;
}
tableeditor.getCheckedRow = function(){
	var result = null; 
	var table = $("_tableeditor_dataTable");
	
	var checkBoxs = table.getElementsByTagName("input");
	for(var i=0; i<checkBoxs.length; i++){
		var e = checkBoxs[i];
		if(e){
			if(e.checked && e.id != '_tableeditor_checkbox'){
				result = e.parentNode.parentNode;
				break;
			}
		}
	}
	return result;
}
tableeditor.getAllCheckedRowIndexs = function(){
	var result = []; 
	if(tableeditor.isUseCheckbox){
		for(var i=0,j=0; i<tableeditor.maxRowNum; i++){
			var e = document.getElementById("_tableeditor_checkbox" + i);
			if(e){
				if(e.checked){
					result.push(j);
				}
				j++;
			}
		}
	}
	return result;
}
tableeditor.getClickedRowIndex = function(row){
	var result = -1; 
	var table = jQuery("#_tableeditor_dataTable")[0];
	var rows = table.rows;
	for(var i=0; i<rows.length; i++){
		if(rows[i] == row){
			result = i - 1;
			break;
		}
	}
	return result;
}
tableeditor.goToPage = function(pageNum){
	var url = window.location.href;
	if(url.indexOf("?") == -1){
		url += "?pageNum=" + pageNum + "&pageSize=" + tableeditor.pageSize;
	}else{
		if(url.indexOf("pageNum=") == -1){
			url += "&pageNum=" + pageNum;
		}else{
			url = url.replace(/pageNum=[0-9]+/, "pageNum=" + pageNum);
		}
		if(url.indexOf("pageSize=") == -1){
		 	url += "&pageSize=" + tableeditor.pageSize;
		}else{
			url = url.replace(/pageSize=[0-9]+/, "pageSize=" + tableeditor.pageSize);
		}
		
	}
	window.location = url;
}
tableeditor.disableIdInput = function(isDisabled){
	if($(tableeditor.id)){
		var idInput = $(tableeditor.id)[tableeditor.IDFieldName];
		if(idInput){
			idInput.disabled = isDisabled;
		}
	}
}
tableeditor.changePageInfo = function(offset){
	var totalNumElement = jQuery("#" + tableeditor.id + "TotalNum")[0];
	if(totalNumElement){
		var totalPageNumElement = jQuery("#" + tableeditor.id + "TotalPageNum")[0];
		var pageSize = tableeditor.pageSize;
		
		var oldTotalNum = tableeditor.totalNum;
		var oldTotalPageNum;
		if(oldTotalNum % pageSize == 0){
			oldTotalPageNum = oldTotalNum / pageSize;
			if(totalPageNum == 0){
				totalPageNum = 1;
			}
		}else{
			oldTotalPageNum = parseInt(oldTotalNum / pageSize) + 1;
		}
		
		tableeditor.totalNum += offset;
		var totalNum = tableeditor.totalNum;
		
		var totalPageNum;
		if(totalNum % pageSize == 0){
			totalPageNum = totalNum / pageSize;
			if(totalPageNum == 0){
				totalPageNum = 0;
			}
		}else{
			totalPageNum = parseInt(totalNum / pageSize) + 1;
		}
		
		totalNumElement.innerHTML = totalNum;
		totalPageNumElement.innerHTML = totalPageNum;
		if(totalPageNum != oldTotalPageNum){ //总页数改变了
			var pageNumList = $('pageNumList');
			pageNumList.innerHTML = "";
			var html = "";
			for(var i=1; i<=totalPageNum; i++){
				if(i != 1){
					html += ",&nbsp;";
				}
				html += '<a ';
				if(tableeditor.pageNum == i){
					html += 'class="currentPage"';
				}
				html += ' onclick="tableeditor.goToPage(' + i + ');">' + i + '</a>';
			}	
			pageNumList.innerHTML = html;		
		}
	}
}
tableeditor.reshowList = function(){
	var table = $("_tableeditor_dataTable");
	
	var rows = table.rows;
	
	var isUseSubField = tableeditor.isUseSubField();
	tableeditor.removeAllTableRow();
	for(var i=0; i<tableeditor.list.length; i++){
		var data = tableeditor.list[i];
		tableeditor.addNewRow(i + 1, data, false, true);
	}
}
/**
 * delete all row from table
 */
tableeditor.removeAllTableRow = function(){
	var table = $("_tableeditor_dataTable");
	var rows = table.rows;
	var leftRowNum = 1;
	if($('_tableeditor_page_row') != undefined){
		leftRowNum = 2;
	}
	while(rows.length > leftRowNum){
		table.deleteRow(1);
	}
}
tableeditor.sortByColumn = function(columnName, isAscend){
	var subField = columnName;
	if(tableeditor.isUseSubField()){
		subField = Utils.getSubField(columnName);
	}
	if(subField == undefined || subField == ''){
		return;
	}
	tableeditor.list = tableeditor.list.sort(
					function(a, b){
						var l = eval("(a." + subField + ")"), r = eval("(b." + subField + ")");
						var t1 = parseFloat(l), t2 = parseFloat(r);
						if(!isNaN(t1) && !isNaN(t2)){
							l = t1;
							r = t2;
						}
						if(l == undefined){
							l = '';
						}
						if(r == undefined){
							r = '';
						}
						if(l < r){
							if(isAscend)
								return -1;
							else
								return 1;
						}else if(l > r){
							if(isAscend)
								return 1;
							else
								return -1;
						}else{
							return 0;
						}
					});
	tableeditor.reshowList();			
}
tableeditor.columnHeadOnClick = function(target, columnName, isAscend){
	tableeditor.sortByColumn(columnName, isAscend);
	target.onclick = tableeditor.columnHeadOnClick.bind(this, target, columnName, !isAscend);
}
/**
 * 重置表单
 * @param type
 * 			1: 所有输入框置为空
 * 			2: 所有输入框置为当前选中的对象的值
 */
tableeditor.resetForm = function(type){
	var form = jQuery("#" + tableeditor.id)[0];
	if(form == undefined){
		return;
	}
	var elements = form.elements;
	var e;
	for(var i=0; i<elements.length; i++){
		e = elements[i];
		if(e.type != 'checkbox' && e.type != 'radio' && tableeditor.allColumns.indexOf(e.name) != -1){
			if(e.tagName == 'SELECT'){
				if(e.options.length > 0){
					e.value = e.options[0].value;
				}				
			}else{
				elements[i].value = "";
			}
		}
	}
	switch(type){
		case 1:
			break;
		case 2:
			var checkedRowIndex = tableeditor.getCheckedRowIndex();
			if(checkedRowIndex == -1){
				alert(tableeditor.checkItemToEditMessage);
				return;
			}
			tableeditor.fillFormForEdit(checkedRowIndex);
			tableeditor.hightEdittingRow();
			break;
		default:
	}
}
tableeditor.changeRowOrder = function(rownum, type){
	var data = jQuery("#" + tableeditor.id).formSerialize();
	data += "&rownum=" + rownum + "&type=" + type;	jQuery.post(
		tableeditor.changeOrderUrl,
		data,
		function(dataFromServer){
			dataFromServer = eval("(" + dataFromServer + ")");
			if(dataFromServer[0] == "success"){
				var table = jQuery("#_tableeditor_dataTable")[0];
				var rows = table.rows;
				if(dataFromServer[4].row1 != undefined){
					tableeditor.list[rownum] = dataFromServer[4].row1;
				}
				if(dataFromServer[4].row2 != undefined){
					tableeditor.list[rownum + type] = dataFromServer[4].row2;
				}
				tableeditor.updateRow(rownum, tableeditor.list[rownum + type], 2);
				tableeditor.updateRow(rownum + type, tableeditor.list[rownum], 2);
				var t = tableeditor.list[rownum];
				tableeditor.list[rownum] = tableeditor.list[rownum + type];
				tableeditor.list[rownum + type] = t;
				tableeditor.onRowClick(rows[rownum + 1]);
			}else{
				Utils.showAjaxTip(dataFromServer);
			}
		}
	)
}
/**
 * 动态增加button
 * @param title
 * 			button的标题
 * @param onclick
 * 			button的onclick事件处理函数
 * @param type
 * 			1: 只有下面的列表中有被选中的行时才可用，否则不可用
 * 			２: 任何时候都可用
 */
tableeditor.addButton = function(value, onclick, type){
	if(type == undefined){
		type = 1;
	}
	var id = '_tableeditor_button_' + new Date().getTime();
	
	var buttonBar = $('_tableeditor_button_bar');
	
	var button = document.createElement("input");
	button.type = 'button';
	button.className = (type==1?'buttonDisabled':'button');
	button.id = id;
	button.value = value;
	button.disabled = (type==1);
	button.style.marginLeft = '4px';
	
	if(buttonBar){
		
		if(type == 1){
			button.onclick = 
					function(){
						var checkedRowIndex = tableeditor.getClickedRowIndex(tableeditor.getCheckedRow());
						if(checkedRowIndex == -1){
							onclick(-1);
						}else{
							var row = tableeditor.list[checkedRowIndex];
							var subField = tableeditor.IDFieldName;
							if(tableeditor.isUseSubField()){
								subField = Utils.getSubField(tableeditor.IDFieldName);
							}
							var objId = row[subField];
							onclick(objId);
						}
					}
			var t = [];
			t[0] = button;
			t[1] = button.onclick;
			tableeditor.extraButtons.push(t);					
		}else{
			button.onclick = onclick;
		}
		buttonBar.appendChild(button);
	}
}
/**
 * @param buttonName
 * 			按钮的名字：
 * 				view: 查看
 * 				edit: 编辑
 * 				delete: 删除
 * @param CBFunction
 * 			按钮点击的回调函数，参数为当前选中的行的对象的id(id)
 */
tableeditor.registerButtonOnclickCBFunction = function(buttonName, CBFunction){
	tableeditor.buttonOnclickCBFunctions[buttonName] = CBFunction;
}
/**
 * 获取日期的日期部分，去掉时间
 * eg.
 * 		2008-06-08T00:00:00 返回 2008-06-08
 * getDateStr
 */
tableeditor.formatDateStr = function(dateStr, format){
	var result;
	if(format == undefined){
		format = "yyyy-MM-dd";
	}
	result = dateStr.replace("T", " ");
	var parsedDate = Date.parse(result, "yyyy-MM-dd HH:mm:ss");
	result = parsedDate.format(format);
	return result;
}
/**
 * 判断str是否是一个日期的字符串
 */
tableeditor.isDateStr = function(str){
	if(str != undefined && str.length == 19){
		var re = /[\d]{4}-[\d]{2}-[\d]{2}T[\d]{2}:[\d]{2}:[\d]{2}/;
		if(re.test(str)){
			return true;
		}
	}
	return false;
}

/**
 * 判断是否需要使用subField(只要一个column不要使用subField则全部不使用)
 */
tableeditor.isUseSubField = function(){
	var result = true;
	for(var i=0; i<tableeditor.allColumns.length; i++){
		var column = tableeditor.allColumns[i];
		if(Utils.getSubField(column) == column){
			result = false;
			break;
		}
	}
	return result;
}
//TODO
tableeditor.childTableColumnHeadOnClick = function(){
	
}
/**
 * 显示某一行的子列表
 * @param index
 * 			要显示的行的index
 */
tableeditor.showChild = function(index){
	if(tableeditor.getChildTableDataUrl != 'null'){
		var row = tableeditor.list[index];
		var subField = tableeditor.IDFieldName;
		if(tableeditor.isUseSubField()){
			subField = Utils.getSubField(tableeditor.IDFieldName);
		}
		var id = row[subField];
		
		jQuery.post(
			tableeditor.getChildTableDataUrl,
			{'id':id},
			function(dataFromServer){
				dataFromServer = eval('(' + dataFromServer + ')');
				if(dataFromServer[0] == 'success'){
					tableeditor.displayChildData(dataFromServer[4].children);
				}else{
					Utils.showAjaxTip(dataFromServer);
				}
			}
		)
	}
}
/**
 * display the child data in the child table
 * @param children
 * 			the data array
 */
tableeditor.displayChildData = function(children){
	tableeditor.removeAllChildTableRow();
	var child;
	if(children){
		for(var i=0; i<children.length; i++){
			child = children[i];
			tableeditor.addNewChildTableRow(i + 1, child);
		}
	}
}
/**
 * add new row to the child table
 * @param index
 * 			the position to insert
 * @param row
 * 			row data
 */
tableeditor.addNewChildTableRow = function(index, row){
	var table = jQuery("#_tableeditor_dataTable2")[0];
	var nTR = table.insertRow(index);
	nTR.onmouseover = tableeditor.trOnMouseOver.bind(this, nTR);
	nTR.onmouseout = tableeditor.trOnMouseOut.bind(this, nTR);
	nTR.ondblclick = tableeditor.onChildTableRowClick.bind(this, nTR);
	var id = row[tableeditor.childDataIDFieldName];
	for(var i=0; i<tableeditor.childTableColumns.length; i++){
		var column = tableeditor.childTableColumns[i];
		var columnInnerHTML = tableeditor.childTableColumnInnerHTMLs[i];
		var nTD = tableeditor.childTableColumnTds[i].cloneNode(false);
		try{
			var t = '';
			try{
				if(column != ''){
					t = eval("(row." + column + ")");
					if(tableeditor.isDateStr(t)){
						t = tableeditor.formatDateStr(t, tableeditor.childTableColumnDateFormats[i]);
					}
				}
			}catch(e){
				
			}
			if(columnInnerHTML != ''){
				columnInnerHTML = tableeditor.fillIdParameter(columnInnerHTML, id);
			}
			if(t == undefined){
				t = '';
			}
			nTD.innerHTML = columnInnerHTML + t;
		}catch(e){
			alert(e);
			
		}
		
		nTR.appendChild(nTD);
	}
}
tableeditor.onChildTableRowClick = function(){
	
}
/**
 * delete all row from child table
 */
tableeditor.removeAllChildTableRow = function(){
	var table = $("_tableeditor_dataTable2");
	var rows = table.rows;
	while(rows.length >1){
		table.deleteRow(1);
	}
}
/**
 * fill id parameter to the links in html
 * @param html
 * 			html code
 * @return
 * 			return the html with it's all links added the id parameter
 * 
 */
tableeditor.fillIdParameter = function(html, id){
	var result = html.replace(/\&\{id\}/g, id);
	return result;
}
tableeditor.showDefaultInEditing = function(){
	$('_tableeditor_checkbox1').checked = true;
	tableeditor.onEditButtonClick();
}